/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package intopo.demonstrator.partnerclassification;

import util.math.Rational;

import java.io.PrintStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.BufferedOutputStream;

/**
 * Main class of the application that performs a batch run of the partner classification algorithm.  It takes arguments from the command line specifying the values of the model. It writes in a text file the results.
 * @author <a href="mailto:mariano.pedro@gmail.com">Pedro Mariano</a>
 *
 * @version 1.0 __DATE__
 */
public class Main
{
	static public void main (String[] args)
	{
		Model model;
		int numberRuns;
		int numberTransitions;
		int numberBitsProbability;
		int poolSize;
		Rational delta;
		int numberBitsWeight;
		int numberPartners;
		Object[] partners;
		PrintStream output;
		try {
			numberRuns = Integer.parseInt (args [0]);
			numberTransitions = Integer.parseInt (args [1]);
			// model parameters
			numberBitsProbability = Integer.parseInt (args [2]);
			poolSize = Integer.parseInt (args [3]);
			delta = new Rational (args [4]);
			numberBitsWeight = Integer.parseInt (args[5]);
			numberPartners = Integer.parseInt (args[6]);
			partners = new Object [args.length - 7];
			int i = 7;
			while (i < args.length) {
				partners [i - 7] = new Rational (args [i]);
				i++;
				partners [i - 7] = Integer.parseInt (args [i]);
				i++;
			}
			StringBuilder filename = new StringBuilder (7 * 10);
			filename.append (args [1]);
			i = 2;
			while (i < 7) {
				filename.append ('_');
				filename.append (args [i]);
				i++;
			}
//			System.out.println ("[" + filename.toString() + "]");
			output = new PrintStream (
				new BufferedOutputStream (
					new FileOutputStream (
						filename.toString().replace ('/', '\\'),
						true),
					4096));
			model = new Model ();
			model.parameters = new Parameters (numberBitsProbability, poolSize, delta, numberBitsWeight, numberPartners, partners);
			for (i = numberRuns; i > 0; i--) {
				model.state = new State (model.parameters);
				model.statistics = new Statistics (model.parameters);
				for (int t = numberTransitions; t > 0; t--) {
					model.state.applyTransition (model.statistics);
				}
				output.println (model.statistics);
			}
			output.close ();
			return ;
		}
		catch (FileNotFoundException fnfe) {
			System.err.println ("Could not open output file");
		}
		catch (NumberFormatException nfe) {
			System.err.println ("Error parsing argument");
		}
		catch (IndexOutOfBoundsException ioobe) {
			System.err.println ("Not enough arguments");
		}
		System.err.println ("Usage:\njava intopo.demonstrator.partnerclassification.Main NUMBER_RUNS NUMBER_TRANSITIONS numberBitsProbability poolSize delta numberBitsWeight numberPartners partners\nwhere");
		System.err.println ("NUMBER_RUNS            a positive integer");
		System.err.println ("NUMBER_TRANSITIONS     a positive integer");
		System.err.println ("numberBitsProbability  a positive integer");
		System.err.println ("poolSize               a positive integer");
		System.err.println ("delta                  a rational number or a floating point number between 0 and 1");
		System.err.println ("numberBitsWeight       a positive integer ");
		System.err.println ("numberPartners         a positive integer ");
		System.err.println ("partners               an array consisting of pairs");
		System.err.println ("                       first element of pair is a rational number or a floating point number between 0 and 1");
		System.err.println ("                       second elemnt of pair is a positive integer");
		System.exit (1);
	}

	private static class Model
	{
		Parameters parameters;
		State state;
		Statistics statistics;
	}
}


// Local Variables:
// mode: java
// mode: flyspell-prog
// ispell-local-dictionary: "british"
// End:
